<template>
	<view class="wp">
		<navigator url="/pages/center/card" hover-class="none">
			<view class="kalist" v-if="bankinfo.cardno">
				<view class="span">银行卡</view>
				<view class="em">
					{{bankinfo.bankname}}（****{{bankinfo.cardno.substring(bankinfo.cardno.length-4,bankinfo.cardno.length)}}）
				</view>
			</view>
		</navigator>
		<view class="web_out_con">
			<view class="span">提现金额</view>
			<view class="fill">
				<view class="em">￥</view>
				<input type="number" v-model="money" placeholder="请输入提现金额">
			</view>
			<view class="mass">钱包余额 <text>￥{{userinfo.balance}}</text>，<text class="b"
					@click="All(userinfo.balance)">全部提现</text></view>
			<div class="btn" @click="Sub()">确定提现</div>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bankinfo: [],
				userinfo: [],
				money: '',
			}
		},
		onLoad() {
			let that = this;
		},
		onShow() {
			let that = this;
			if (that.common.GetStorage('byh5token')) {
				that.logintype = that.common.GetStorage('byh5type');
				that.GetUserMass();
			} else {
				uni.reLaunch({
					url: '/pages/login/login',
				})
			}
		},
		methods: {

			Sub: function() {
				let that = this;
				if (that.money == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入提现金额',
						duration: 2000
					});
				} else if (that.money > that.userinfo.balance) {
					uni.showToast({
						icon: 'none',
						title: '余额不足',
						duration: 2000
					});
				} else {
					uni.showLoading({
						title: '提交中...',
						mask: true
					});
					let _data = {
						fee:that.money,
						bank_id:that.bankinfo.id
					}
					that.common.http('POST', 'user/withdraw', _data, function(res) {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: '提现申请已提交！',
							duration: 2000
						});
						setTimeout(function() {
							that.navigateBack();
						}, 1500);
					});
				}
			},

			All(num) {
				let that = this;
				if (num == 0) {
					uni.showToast({
						icon: 'none',
						title: '余额为0不可提现',
						duration: 2000
					});
				} else {
					that.money = num;
				}
			},
			GetUserBank: function() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.common.http('GET', 'user/bank', '', function(res) {
					uni.hideLoading();
					console.log(res);
					if(res.data){
						that.bankinfo = res.data;
					}
					else{
						uni.showToast({
							icon: 'none',
							title: '请先完善银行卡信息',
							duration: 2000
						});
						setTimeout(function() {
							uni.navigateTo({
								url:'/pages/center/card'
							})
						}, 1500);
					}
					
					
				});
			},
			GetUserMass: function() {
				let that = this;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				that.common.http('GET', 'user', '', function(res) {
					uni.hideLoading();
					console.log(res)
					
					
					that.userinfo = res.data
					that.GetUserBank();
				});
			},
			
			navigateBack() {
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				//使用vue-router返回上一级
				let a = this.$router.go(-1)
				if (a == undefined) {}
				return;
				// #endif
				uni.navigateBack();
			},
			
		}
	}
</script>

<style scoped>
	.wp {
		background-color: #fff;
		padding-bottom: 40rpx;
	}

	.kalist {
		height: 100rpx;
		align-items: center;
		line-height: 1;
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #ebebeb;
		padding-right: 30rpx;
		background-image: url('@/static/images/app_i3.png');
		background-repeat: no-repeat;
		background-size: 20rpx auto;
		background-position: right center;
	}

	.kalist .span {
		font-size: 32rpx;
		color: #000;
	}

	.kalist .em {
		font-size: 28rpx;
		color: #444;
	}
</style>